
import router from '@/router/index.js'

export default {
    namespaced: true,
    state: {
        token: sessionStorage.getItem('token') || '',
        permissionList: [],
        sidebarMenu: [],
        currentMenu: sessionStorage.getItem('currentMenu') || ""
    },
    getters: {},
    mutations: {
        LOGIN(state, token) {

            state.token = token
            sessionStorage.setItem('token', token)

        },
        SET_PERMISSION(state, routes) {

            state.permissionList = routes

        },
        CLEAT_PERMISSiON(state) {
            state.permissionList = []
        },
        SET_MENU(state, menu) {
            state.sidebarMenu = menu
        },
        CLEAR_MENU(state) {
            state.sidebarMenu = []
        },
        SET_CURRENTMENU(state, data) {
            state.currentMenu = data
            sessionStorage.setItem('currentMenu', data)
        }
    },
    actions: {
        async FETCH_PERMISSION({ commit, state },) {

            // let premissionList = await getPermissionAPI()



            let children = []
            let MainContainer = { children: [] }
            let routes = []
            routes = recursionRouter(premissionList.data, dynamicRouter)
            console.log(premissionList.data);
            console.log(dynamicRouter);
            console.log(routes, 8888);
            MainContainer = DynamicRoutes.find(v => v.name == '/')

            MainContainer.children = []
            children = MainContainer.children
            children.push(...routes)
            // 生成菜单

            commit('SET_MENU', children)
            // 设置默认路由
            setDefaultRoute([MainContainer])
            // 初始化路由
            let initRoutes = router.options.routes
            console.log(DynamicRoutes);
            DynamicRoutes.forEach(v => {
                router.addRoute(v)
            })


            commit('SET_PERMISSION', [...initRoutes, ...DynamicRoutes])
        },

        // 退出
        logout({ commit, state }) {
            console.log(222);

            let BASE_URL = localStorage.getItem('BASE_URL')
            localStorage.clear()
            sessionStorage.clear()
            localStorage.setItem('BASE_URL', BASE_URL)

            commit('LOGIN', '')
            // router.replace('/login')
            router.push('/login')

        }
    }
}